package org.lex.io.filesystem.tools;

import java.io.File;

import org.lex.filter.Filter;
import org.lex.io.filesystem.traveller.FileProcessor;
import org.lex.io.filesystem.traveller.FileTraveller;


public class DeleteSVNDir implements Runnable {
	public static void main(String[] args) {
		new DeleteSVNDir("E:\\Projects\\JavaEE\\MTV_L").run();
	}

	private File root = null;

	public DeleteSVNDir(String root) {
		this.root = new File(root);
	}

	public void run() {
		FileTraveller<Boolean> t = new FileTraveller<Boolean>(root, new SVNDirFilter(), new DeleteSVNDirProcessor());
		System.out.println("Start on: " + root.getAbsolutePath());
		t.run();
		System.out.println("Done!");
	}

	class SVNDirFilter implements Filter<File> {
		public boolean accept(File file) {
			if (null == file || false == file.exists())
				return false;
			// return this.fileNameFilter( file, "Makefile" );
			// return this.fileFilter( file, ".pdb" );
			// return this.fileFilter( file, ".ilk", ".pro", ".sln", ".vcproj",
			// ".obj", ".Debug", ".Release", ".exp");
			return this.directoryFilter(file, "CVS");
		}

		boolean fileNameFilter(File file, String name) {
			if (false == file.isFile())
				return false;
			return name.equals(file.getName());
		}

		boolean directoryFilter(File file, String... names) {
			if (false == file.isDirectory())
				return false;
			String name = file.getName();
			for (String n : names)
				if (n.equals(name))
					return true;
			return false;
		}

		boolean fileFilter(File file, String... postfixs) {
			if (false == file.isFile())
				return false;
			String name = file.getName();
			for (String pf : postfixs)
				if (name.endsWith(pf))
					return true;
			return false;
		}
	}

	class DeleteSVNDirProcessor implements FileProcessor<Boolean> {
		public Boolean operate(File file) {
			// this.deleteFile( file );
			this.deleteDirectory(file);
			System.out.println(file.getAbsolutePath());
			return null;
		}

		void deleteFile(File file) {
			file.delete();
		}

		void deleteDirectory(File dir) {
			new DeleteDirectory(dir).run();
		}
	}
}
